package client;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.*;
import util.*;

public class ClientSocketThread extends SocketThread {
	
	/**
	 * Initialize the SocketThread
	 * @param serverSocket a socket to the server
	 * @throws UnsupportedEncodingException if client doesn't support UTF-8
	 * @throws IOException from the in-/output streams
	 */
	public ClientSocketThread(Socket serverSocket) throws UnsupportedEncodingException, IOException{
		super(serverSocket);
		
	}
	
	/**
	 * Begin reading the input from the socket.
	 */
	public void run() {
		try {
			socket.setKeepAlive(true);
			String inputLine;
			while ((inputLine = in.readLine()) != null){
				Client.processServerInput(inputLine);
				yield();
			}
		} catch (IOException e) {
				// Connection reset
		} finally {
			if (!socket.isClosed())
				close();
			this.connectionClosed = true;
		}
	}

	/**
	 * Close the connection
	 */
	public void close() {
		try {
			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//Client.resetConnection();
	}
}
